package com.winter.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jiguang.common.utils.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.winter.mapper.AreaCodeMapper;
import com.winter.mapper.CompanyMapper;
import com.winter.mapper.CompanyPositionMapper;
import com.winter.mapper.DictEntryMapper;
import com.winter.mapper.IcompanyCardMapper;
import com.winter.mapper.IeliteRelainfoMapper;
import com.winter.model.AreaCode;
import com.winter.model.Company;
import com.winter.model.DictEntry;
import com.winter.model.IcompanyCard;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

@Service
public class IcompanyCardService {

	@Autowired
	private IcompanyCardMapper icompanyCardMapper;

	@Autowired
	public IeliteRelainfoMapper ieliteRelainfoMapper;
	
    @Autowired
    private DictEntryMapper dictEntryMapper;
    
    @Autowired
    private AreaCodeMapper areaCodeMapper;
     
    @Autowired
    private CompanyMapper companyMapper;
    
    @Autowired
    private CompanyPositionMapper companyPositionMapper;

	/*
	 * 公司圈列表
	 */
	public Result icompanyCardList(Integer pageSize, Integer pageNumber,
			String comClass, String comSubClass, String userNo, String key) {
		if (pageNumber == null) {
			pageNumber = Constants.PAGENUMBER;
		}
		if (pageSize == null) {
			pageSize = Constants.PAGESIZE;
		}
		String companyIndustry = null;
	    if (!Strings.isNullOrEmpty(key)) {
	        companyIndustry = dictEntryMapper.queryDictNameLike(key);
		}
		String orderBy = "createtime desc";
		List<IcompanyCard> ic1 = icompanyCardMapper.icompanyCardList(orderBy,
				comClass, comSubClass, key,companyIndustry);
		PageHelper.startPage(pageNumber, pageSize);
		List<IcompanyCard> ic = icompanyCardMapper.icompanyCardList(orderBy,
				comClass, comSubClass, key,companyIndustry);
		for (IcompanyCard ui : ic) {
			if (StringUtils.isNotEmpty(userNo)) {
				int i = ieliteRelainfoMapper.getIeliteIsColln(ui.getId(),
						userNo);
				if (i > 0) {
					ui.setIsCollection("1");
				} else {
					ui.setIsCollection("0");
				}
			} else {
				ui.setIsCollection("0");
			}
			getTypeName(ui);
		}
		PageInfo<IcompanyCard> pageInfo = new PageInfo<IcompanyCard>(ic);
		pageInfo.setTotal(ic1.size());
		return Result.success(pageInfo);
	}

	/*
	 * 通过id查询公司圈信息
	 */
	public Result getIcomCard(Long id, String userNo) {
		if (Strings.isNullOrEmpty(userNo)) {
			return Result.failure(ResultCode.FAILURE, "用户未登录,请登录");
		}
		IcompanyCard ic = icompanyCardMapper.selectByPrimaryKey(id);
		if (null != ic) {
			int i = ieliteRelainfoMapper.getIeliteIsColln(ic.getId(), userNo);
			if (i > 0) {
				ic.setIsCollection("1");
			} else {
				ic.setIsCollection("0");
			}
			getTypeName(ic);
		}
		int count = companyPositionMapper.queryPageCount(ic.getId());
		ic.setFbPanyPostionCount(count);
		return Result.success(ic);
	}
	
    public void getTypeName(IcompanyCard company) {
        DictEntry dictEntry = dictEntryMapper.get("DC_INDU_CLASSIFY", company.getCompanyIndustry());
        if (dictEntry != null) {
            company.setCompanyIndustry(dictEntry.getDictname());
        }
        DictEntry dictEntry1 = dictEntryMapper.get("DC_COM_CLASSIFY", company.getCompanyNature());
        if (dictEntry1 != null) {
            company.setCompanyNature(dictEntry1.getDictname());
        }
        DictEntry dictEntry2 = dictEntryMapper.get("DC_COMPANY_SCALE", company.getCompanyScale());
        if (dictEntry2 != null) {
            company.setCompanyScale(dictEntry2.getDictname());
        }
        if (company.getComProId() != null) {
            AreaCode areaCode = areaCodeMapper.selectByPrimaryKey(company.getComProId());
            if (areaCode != null) {
                company.setComProName(areaCode.getChiShortName());
            }
        }
        if (company.getComCityId() != null) {
            AreaCode areaCode = areaCodeMapper.selectByPrimaryKey(company.getComCityId());
            if (areaCode != null) {
                company.setComCityName(areaCode.getChiShortName());
            }
        }
        if (!Strings.isNullOrEmpty(company.getFinClass())) {
        	DictEntry dictEntry4 = dictEntryMapper.get("DC_DJS_ORG_FIN", company.getFinClass());
        	if(dictEntry4 != null){
        		company.setFinClassName(dictEntry4.getDictname());
        	}
        	if(!Strings.isNullOrEmpty(company.getFinSubclass())){
        		if (company.getFinClass().equals("1")) {
        			DictEntry dictEntry5 = dictEntryMapper.get("DC_DJS_ORG_FIN_BANK", company.getFinSubclass());
        			if(dictEntry5 != null){
                		company.setFinClassName(company.getFinClassName()+"-"+dictEntry5.getDictname());
                	}
    			}else if(company.getFinClass().equals("6")){
    				DictEntry dictEntry6 = dictEntryMapper.get("DC_DJS_ORG_FIN_FUND", company.getFinSubclass());
    				if(dictEntry6 != null){
    	        		company.setFinClassName(company.getFinClassName()+"-"+dictEntry6.getDictname());
    	        	}
    			}
        	}
		}
    }
}
